package com.hans.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.hans.diolog.R;

/**
 * Created by Administrator on 2016/6/15.
 */
public class CustomAlterDialog extends CustomDialog {
    private TextView dialogTitle,dialogMessage;
    private View confirmBtn,cancelBtn;
    private View btnLayout;

    public CustomAlterDialog(Context context) {
        super(context,R.layout.custom_alert_dialog);
        findView();
        initView();
    }

    private void findView(){
        dialogTitle = (TextView) findViewById(R.id.dialog_title);
        dialogMessage = (TextView) findViewById(R.id.dialog_message);
        confirmBtn = findViewById(R.id.confirm_btn);
        cancelBtn = findViewById(R.id.cancel_btn);
        btnLayout = findViewById(R.id.btn_layout);
    }

    private void initView(){
        confirmBtn.setOnClickListener(onClickListener);
        cancelBtn.setOnClickListener(onClickListener);
    }

    public void setDialogTitle(String titleText){
        dialogTitle.setText(titleText == null ? "" : titleText);
    }

    public void setDialogTitle(int titleResId){
        if(context.getString(titleResId) == null) dialogTitle.setText("");
        else dialogTitle.setText(titleResId);
    }


    public void setDialogMessage(String titleText){
        dialogMessage.setText(titleText == null ? "" : titleText);
    }

    public void setDialogMessage(int titleResId){
        if(context.getString(titleResId) == null) dialogMessage.setText("");
        else dialogMessage.setText(titleResId);
    }

    public void setCancleBtnFromLayout(int viewResId){
        btnLayout.setVisibility(View.GONE);//btnLayout隐藏
        cancelBtn = findViewById(viewResId);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCustomDialogClickListener.onCancelBtnClicked();
                dismiss();
            }
        });
    }

    public void setConfirmBtnFromLayout(int viewResId){
        btnLayout.setVisibility(View.GONE);
        confirmBtn = findViewById(viewResId);
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCustomDialogClickListener.onConfirmBtnClicked();
                dismiss();
            }
        });
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(onCustomDialogClickListener == null) return;
            int i = v.getId();
            if (i == R.id.confirm_btn) {
                onCustomDialogClickListener.onConfirmBtnClicked();
                dismiss();

            } else if (i == R.id.cancel_btn) {
                onCustomDialogClickListener.onCancelBtnClicked();
                dismiss();
            }
        }
    };

    private OnCustomDialogClickListener onCustomDialogClickListener;

    public void setOnCustomDialogClickListener(OnCustomDialogClickListener onCustomDialogClickListener) {
        this.onCustomDialogClickListener = onCustomDialogClickListener;
    }
    public interface OnCustomDialogClickListener {
        void onConfirmBtnClicked();
        void onCancelBtnClicked();
    }
}
